package com.data.controllers;

import com.data.dto.TableData;
import com.data.dto.TableHead;
import com.data.service.DataService;
import com.data.controllers.response.BaseResponse;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/")
public class SchemaController {

    @Autowired
    private DataService dataService;

    @PostMapping("listTables")
    public BaseResponse listTables(@RequestParam(value = "schema", required = false, defaultValue = "mysql")String schema){
        List result = dataService.listTables(schema);
        return BaseResponse.build(result,result.size());
    }

    @PostMapping("listSchemas")
    public BaseResponse listSchemas(){
        List result = dataService.listSchemas();
        return BaseResponse.build(result,result.size());
    }

    @PostMapping("listTableData")
    public BaseResponse listTableData(@RequestParam(value = "schema", required = true, defaultValue = "mysql")String schema,
                                      @RequestParam(value = "table", required = true, defaultValue = "test")String table,
                                      @RequestParam(value = "parse", required = true, defaultValue = "false")boolean parse,
                                      @RequestParam(value = "pageNum", required = false, defaultValue = "1")Integer pageNum,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10")Integer pageSize,
                                      @RequestParam(value = "totalSize", required = false, defaultValue = "0")Integer  totalSize){

        totalSize = dataService.getAllCount(schema, table);

        List<TableHead> columnList = dataService.listTableColumn(schema, table);
        PageInfo<Map<String,Object>> list = dataService.listTableData(schema, table, parse, pageNum,pageSize);
        Map<String, Object> m = new HashMap<>();
        m.put("columns", columnList);
        m.put("data",list.getList());
        m.put("totalSize",totalSize);
        return BaseResponse.build(m, (int) list.getTotal());
    }

    @PostMapping("createSchema")
    public BaseResponse createSchema(String schema){
        Object result = dataService.createSchema(schema);

        System.out.println("------result: "+result);
        return BaseResponse.build("",1);
    }


    @PostMapping("createTable")
    public BaseResponse createTable(String createTable){
        Object result = dataService.createTable(createTable);

        System.out.println("------result: "+result);
        return BaseResponse.build("",1);
    }

    @PostMapping("createData")
    public BaseResponse createData(TableData data){
        dataService.createData(data);
        return BaseResponse.build("",1);
    }

    @PostMapping("printData")
    public BaseResponse printData(@RequestParam(value = "id", required = true, defaultValue = "0")String id){
        dataService.printData(id);
        return BaseResponse.build("",1);
    }
}
